package com.hiss.basic.starter.security.helper;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.SaLoginModel;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.stp.parameter.SaLoginParameter;
import cn.hutool.core.util.ObjectUtil;
import com.hiss.basic.starter.core.constant.SystemConstant;
import com.hiss.basic.starter.security.model.AuthenticationModel;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class SecurityHelper {

    public static final String SESSION_USER_INFO_FIELD = "session_user_info";
    public static final String USER_ID_FIELD = "user_id";
    public static final String USERNAME_FIELD = "username";
    public static final String NICKNAME_FIELD = "nickname";

    /**
     * 登陆
     *
     * @param auth 认证信息
     */
    public static void login(AuthenticationModel auth) {
        login(auth, null);
    }

    /**
     * 登陆
     *
     * @param auth  认证信息
     * @param model 可实现动态 token 有效期
     */
    public static void login(AuthenticationModel auth, SaLoginParameter model) {
        model = ObjectUtil.defaultIfNull(model, new SaLoginParameter());

        StpUtil.login(auth.getUserId(),
                model.setExtra(USER_ID_FIELD, auth.getUserId())
                        .setExtra(USERNAME_FIELD, auth.getUsername())
                        .setExtra(NICKNAME_FIELD, auth.getNickname()));

        StpUtil.getTokenSession().set(SESSION_USER_INFO_FIELD, auth);
    }

    public static boolean logged() {
        try {
            return ObjectUtil.isNotNull(getAuthentication());
        } catch (Exception e) {
            return false;
        }
    }

    public static AuthenticationModel getAuthentication() {
        SaSession session = StpUtil.getTokenSession();
        if (ObjectUtil.isNotNull(session)) {
            return (AuthenticationModel) session.get(SESSION_USER_INFO_FIELD);
        }
        return null;
    }

    public static AuthenticationModel getAuthentication(String token) {
        SaSession session = StpUtil.getTokenSessionByToken(token);
        if (ObjectUtil.isNotNull(session)) {
            return (AuthenticationModel) session.get(SESSION_USER_INFO_FIELD);
        }
        return null;
    }

    public static boolean isSuperAdmin(Long userId) {
        return SystemConstant.ADMIN_USER_ID.equals(userId);
    }

    public static boolean isSuperAdmin() {
        return SystemConstant.ADMIN_USER_ID.equals(getUserId());
    }

    public static Long getUserId() {
        return Long.valueOf(String.valueOf(getExtra(USER_ID_FIELD)));
    }

    public static String getUsername() {
        return (String) getExtra(USERNAME_FIELD);
    }

    public static String getNickname() {
        return (String) getExtra(NICKNAME_FIELD);
    }

    private static Object getExtra(String key) {
        try {
            return StpUtil.getExtra(key);
        } catch (Exception e) {
            return null;
        }
    }
}
